<?php
/**
 * Created by PhpStorm.
 * User: Shine
 * Date: 2019-02-23
 * Time: 15:17
 */


namespace app\m\controller;

use think\Db;

class Home extends Base
{

    public function index()
    {
        $ses = session('ses');
        $rev = ['err' => 0, 'msg' => ''];
        if ($ses) {
            // 设置账户信息
            $oid = $ses['oid'];
            $qs = Db::query('select nickname,avatar from wx_ac where id=?', [$oid]);
            $rev['user'] = $qs[0];
            // 用户级别 解锁
        } elseif (!$this->ses($rev, $ses)) {
            echo '{"err":1,"msg":"登录失败"}';
            return;
        }
        $oid = $ses['oid'];
        $uid = $ses['uid'];

        // *******************************************************************************************************************************
        // 推荐内容

        $m = isset($_GET['m']) ? intval($_GET['m']) : 0; // 0刷新 ，1加载
        $id = isset($_GET['id']) ? intval($_GET['id']) : 0;
        $more = $m && $id;
        $qs = Db::table('x1_activity a')->field('a.id,u.uid,a.time,a.title,a.content,a.likes,u.name,u.avatar,ur.duty,s.name pname')->join('x1_user_role ur', 'a.rid=ur.id', 'LEFT')->join('user u', 'ur.uid=u.uid', 'LEFT')->join('x1_school s', 'ur.pid=s.id', 'LEFT')->where('a.state=0')->order('a.id desc');

        if ($more) {
            $qs = $qs->where('a.id', 'lt', $id);
        }
        $qs = $qs->limit(0, 5)->select();
        $ids = [];
        $activity = [];
        foreach ($qs as $k => $v) {
            $v['name'] = $v['name'] ? $v['name'] : '';
            $v['content'] = strlen($v['content']) > 40 ? substr($v['content'], 0, 40) . '..' : $v['content'];
            $v['time'] = date('m-d H:i', $v['time']);
            $v['avatar'] = $v['avatar'] ? $v['avatar'] : 'https://c.g56.cn/x9/user_im.png';
            $v['pics'] = [];
            $v['commit'] = [];
            $v['my_likes'] = 0;
            $activity[$v['id']] = $v;
            $ids[] = $v['id'];
        }
        $qs = Db::query('select * from x1_user_role where uid=? and state=0', [$uid]);
        $is_apply = $qs ? 1 : 0;

        if ($ids) {
            //图片
            $pics = Db::query('select id,pid,thumb,pic,type from x1_activity_img where pid in (' . join(',', $ids) . ') and state<3 order by id');
            foreach ($pics as $pic) {
                if (count($activity[$pic['pid']]['pics']) < 3) {
                    $activity[$pic['pid']]['pics'][] = ['id' => $pic['id'], 'thumb' => $pic['thumb'], 'pic' => $pic['pic'], 'type' => $pic['type']];
                }
            }

            //评论
            $commit = Db::query('select c.id,c.time,c.uid,c.pid,c.content,u.name,u.phone from x1_commit c left join user u on c.uid=u.uid where c.pid in (' . join(',', $ids) . ')  and c.state < 3 order by c.id');
            foreach ($commit as $com) {
                if (count($activity[$com['pid']]['commit']) < 3) {
                    $com['time'] = date('Y-m-d H:i', $com['time']);
//                    $com['content'] = strlen($com['content']) ? mb_substr($com['content'], 0, 40) : $com['content'];
                    $activity[$com['pid']]['commit'][] = ['id' => $com['id'], 'uid' => $com['uid'], 'name' => $com['name'], 'phone' => $com['phone'], 'content' => $com['content'], 'time' => $com['time']];
                }
            }
            //自己点赞
            $likes = Db::query('select uid,pid from x1_likes where uid=? and pid in (' . join(',', $ids) . ') and state=1', [$uid]);

            foreach ($likes as $like) {
                $activity[$like['pid']]['my_likes'] = 1;
            }


            //点赞所有人
            $all_likes = Db::query('select u.name,u.uid,l.pid from x1_likes l left join user u on l.uid=u.uid  where l.pid in (' . join(',', $ids) . ') and l.state=1');

            foreach ($all_likes as $k => $a) {
                if (isset($activity[$a['pid']]['likes_name'])) {
                    $activity[$a['pid']]['likes_name'][] = ['uid' => $a['uid'], 'name' => $a['name']];
                } else {
                    $activity[$a['pid']]['likes_name'] = [['uid' => $a['uid'], 'name' => $a['name']]];
                }
            }

            //调整key
            $activity = array_values($activity);
        }
        foreach ($activity as $k => $v) {
            $activity[$k]['likes_name'] = isset($v['likes_name']) ? $v['likes_name'] : [];
        }
        $rev['list'] = $activity;
        $rev['is_apply'] = $is_apply;

        // print_r($rev);

        echo json_encode($rev);
    }

    public function  test(){

        Db::execute("update x1_activity set content='校园足球嘉年华 孩子们玩的很开心' where id=7");

    }


}
